using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_threads_NewReplyPage : Controls.DataManagerThreadControl
{
    private bool _QuotePost;
    public bool QuotePost
    {
        get
        {
            return _QuotePost;
        }
        set
        {
            _QuotePost = value;
        }
    }

    private DAL.Post _currentPost;
    public DAL.Post CurrentPost
    {
        get
        {
            return _currentPost;
        }
        set
        {
            _currentPost = value;
        }
    }

    private DAL.User CurrentUser
    {
        get
        {
            return Managers.UserLoginManager.CurrentUser;
        }
    }

    protected override void ItemChanged()
    {
        if (CurrentThread != null &&
            CurrentUser != null &&
            CurrentThread.Status != DAL.Thread.ThreadStatus.Deleted &&
            Helpers.ForumSecurityHelper.CurrentUserCanViewForum(CurrentThread.Forum))
        {
            if (CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator) ||
                CurrentThread.Status == DAL.Thread.ThreadStatus.Open)
            {
                Breadcrumb1.FooterText = Helpers.StringHelper.SafeFormat(CurrentThread.FirstPost.Subject);
                Breadcrumb1.FooterUrl = Helpers.StringHelper.SafeFormat("~/forums/thread.aspx?t={0}", CurrentThread.ID);
                Breadcrumb1.CurrentForum = CurrentThread.Forum;
                mviewThread.SetActiveView(viewThread);
                ThreadPosts1.SetDataSource(DAL.Posts.GetLast25ByThread(CurrentThread));
            }
            else
            {
                mviewThread.SetActiveView(viewThreadLocked);
            }
        }
        else
        {
            mviewThread.SetActiveView(viewNoThread);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DAL.Post myPost = TryGetPost();
            if (myPost != null)
            {
                CurrentPost = myPost;
                CurrentThread = myPost.Thread;
                if (myPost.Subject.Length > 0)
                {
                    // I'm using String.Format rather than Helpers.StringHelper.SafeFormat
                    // so avoid double-HtmlEncoding text.
                    txtSubject.Text = String.Format("RE: {0}", myPost.Subject);
                }

                if (QuotePost)
                {
                    txtPost.Text = String.Format("[quote={0}]{1}[/quote]", myPost.User.Username, myPost.Content);
                }
            }
            else
            {
                CurrentThread = GetItemFromQueryString();
            }
        }
        if (CurrentThread != null)
        {
            Page.Title = "FSTDT Forums: " + Helpers.StringHelper.SafeFormat("Reply to '{0}'", CurrentThread.FirstPost.Subject);
        }
        else
        {
            Page.Title = "FSTDT Forums: " + "Thread not found";
        }
    }

    private DAL.Post TryGetPost()
    {
        long postID;
        if (Int64.TryParse(Request.QueryString["p"], out postID))
        {
            QuotePost = false;
        }
        else if (Int64.TryParse(Request.QueryString["q"], out postID))
        {
            QuotePost = true;
        }

        return DAL.Posts.Item(postID);
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DAL.User myUser = Managers.UserLoginManager.CurrentUser;
        if (Page.IsValid && myUser != null &&
            (myUser.HasPermissions(DAL.User.UserStatus.Moderator) || CurrentThread.Status == DAL.Thread.ThreadStatus.Open))
        {
            DAL.Post myPost = new DAL.Post();
            myPost.Thread = CurrentThread;
            myPost.Subject = txtSubject.Text;
            myPost.Content = txtPost.Text;
            myPost.User = myUser;
            DAL.Posts.Add(myPost);

            Response.Redirect(string.Format("~/forums/thread.aspx?p={0}#{0}", myPost.ID));
        }
    }
    protected void btnPreview_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            PreviewPost1.SetPreview(txtSubject.Text, txtPost.Text);
        }
    }
}
